package cn.edu.zafu.bigdata.YinMingyan.chapter12;


import java.sql.*;
import java.util.Scanner;

public class StudentInformationTest {
    private static final Scanner scanner = new Scanner(System.in);

    public static void main(String[] args) {
        // 创建数据库表
        try (Connection conn = DataBaseFunction.getConnection()) {
            DataBaseFunction.createTable(conn);
        } catch (SQLException e) {
            System.out.println("Database connection failed: " + e.getMessage());
            return;
        }

        boolean running = true;
        while (running) {
            System.out.println("======= 学生信息管理系统 =======");
            System.out.println("1. 添加学生信息");
            System.out.println("2. 检索学生信息");
            System.out.println("3. 更新学生信息");
            System.out.println("4. 删除学生信息");
            System.out.println("5. 退出程序");
            System.out.print("请输入操作编号 (1-5): ");
            String option = scanner.nextLine();

            try {
                switch (option) {
                    case "1":
                        addStudent();
                        break;
                    case "2":
                        findStudent();
                        break;
                    case "3":
                        updateStudent();
                        break;
                    case "4":
                        deleteStudent();
                        break;
                    case "5":
                        running = false;
                        break;
                    default:
                        System.out.println("无效的输入，请重新输入。");
                        break;
                }
            } catch (SQLException e) {
                System.out.println("Database operation failed: " + e.getMessage());
            }
        }
        scanner.close();
    }

    private static void addStudent() throws SQLException {
        System.out.print("请输入学号：");
        String id = scanner.nextLine();
        while (true) {
            try (Connection conn = DataBaseFunction.getConnection()) {
                if (DataBaseFunction.findStudent(conn, id) != null) {
                    System.out.println("学号重复，请重新输入学号：");
                    id = scanner.nextLine();
                } else {
                    System.out.print("请输入姓名：");
                    String name = scanner.nextLine();
                    System.out.print("请输入电话：");
                    String phone = scanner.nextLine();
                    DataBaseFunction.addStudent(conn, new Student(id, name, phone));
                    return;
                }
            } catch (SQLException e) {
                System.out.println("数据库操作失败: " + e.getMessage());
                throw e;
            }
        }
    }



    private static void findStudent() throws SQLException {
        System.out.print("请输入学号进行检索（输入QUIT退出）：");
        String id = scanner.nextLine();
        while (!id.equalsIgnoreCase("QUIT")) {
            try (Connection conn = DataBaseFunction.getConnection()) {
                Student student = DataBaseFunction.findStudent(conn, id);
                if (student != null) {
                    System.out.println(student);
                } else {
                    System.out.println("未找到该学生");
                }
            }
            id = scanner.nextLine();
        }
    }

    private static void updateStudent() throws SQLException {
        System.out.print("请输入学号进行更新（输入QUIT退出）：");
        String id = scanner.nextLine();
        while (!id.equalsIgnoreCase("QUIT")) {
            try (Connection conn = DataBaseFunction.getConnection()) {
                Student student = DataBaseFunction.findStudent(conn, id);
                if (student != null) {
                    System.out.println("请输入新的姓名：");
                    student.setName(scanner.nextLine());
                    System.out.println("请输入新的电话：");
                    student.setPhone(scanner.nextLine());
                    DataBaseFunction.updateStudent(conn, student);
                    System.out.println("更新成功");
                } else {
                    System.out.println("未找到该学生");
                }
            }
            id = scanner.nextLine();
        }
    }

    private static void deleteStudent() throws SQLException {
        System.out.print("请输入学号进行删除（输入QUIT退出）：");
        String id = scanner.nextLine();
        while (!id.equalsIgnoreCase("QUIT")) {
            try (Connection conn = DataBaseFunction.getConnection()) {
                Student student = DataBaseFunction.findStudent(conn, id);
                if (student != null) {
                    DataBaseFunction.deleteStudent(conn, id);
                    System.out.println("删除成功");
                } else {
                    System.out.println("未找到该学生");
                }
            }
            id = scanner.nextLine();
        }
    }
}
